package gui;

import java.awt.Dimension;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JTextField;

import app.Measurement;
import net.miginfocom.swing.MigLayout;

/********************************************************
 *
 *  Project :  TeamProject
 *  Package :  gui
 *  File    :  IngredientFieldPanel.java
 *  Name    :  Tyler Daines
 *  Date    :  Nov 19, 2013
 *
 ********************************************************/
public class IngredientFieldPanel extends JPanel implements FocusListener
{
	private JComboBox<Measurement> measurementBox;
	private Measurement[] measurements;
	private JCheckBox ingredientCheckBox;
	private JTextField ingredientField;
	private JTextField measurementAmountField;
	
	
	
	/********************************************************
	 * Name       : IngredientFieldPanel
	 * Purpose    : Constructor
	 * Parameters : 
	 ********************************************************/
	public IngredientFieldPanel(String name, Float amount, Measurement measurement)
	{
		setLayout(new MigLayout());
		
		
		initField(name, amount);
		initCheck();
		initCombo(measurement);
		addComponents();
		setBorder(null);
		this.setMaximumSize(new Dimension(330, 40));
		
	}
	
	/********************************************************
	* Name       : initField
	* Purpose    : 
	* Parameters : 
	* Returns    : void
	********************************************************/
	private void initField(String name, Float amount)
	{
		ingredientField = new JTextField(name);
		ingredientField.setMinimumSize(new Dimension(150, 25));
		ingredientField.setMaximumSize(new Dimension(150, 25));
		ingredientField.addFocusListener(this);
		
		measurementAmountField = new JTextField(Float.toString(amount));
		measurementAmountField.setMinimumSize(new Dimension(50, 25));
		measurementAmountField.setMaximumSize(new Dimension(50, 25));
		measurementAmountField.addFocusListener(this);
		
	}
	/********************************************************
	* Name       : initCheck
	* Purpose    : 
	* Parameters : 
	* Returns    : void
	********************************************************/
	private void initCheck()
	{
		ingredientCheckBox = new JCheckBox();
		
	}
	/********************************************************
	* Name       : initCombo
	* Purpose    : 
	* Parameters : 
	* Returns    : void
	********************************************************/
	private void initCombo(Measurement measurement)
	{
		measurementBox = new JComboBox<>();
		measurements = Measurement.values();
		for(Measurement ele : measurements)
			measurementBox.addItem(ele);
		
		measurementBox.setSelectedIndex(measurement.ordinal());
	}
	
	/********************************************************
	* Name       : addComponents
	* Purpose    : 
	* Parameters : 
	* Returns    : void
	********************************************************/
	private void addComponents()
	{
		add(ingredientCheckBox);
		add(measurementAmountField);
		add(measurementBox);
		add(ingredientField);
		
	}
	

	@Override
	public String getName()
	{
		// TODO Auto-generated method stub
		return ingredientField.getText();
	}
	
	public Float getAmount()
	{
		return Float.parseFloat(measurementAmountField.getText());
	}
	
	public Measurement getMeasurement()
	{
		return (Measurement) measurementBox.getSelectedItem();
	}
	
	public JCheckBox getIngredientCheckBox()
	{
		return ingredientCheckBox;
	}
	
	public void setAmount(Float newAmount)
	{
		String newAmountString = String.format("%.3f", newAmount);
		measurementAmountField.setText(newAmountString);
	}

	
	@Override
	public void focusGained(FocusEvent e)
	{
		if(e.getSource() == measurementAmountField)
		{
			measurementAmountField.selectAll();
		}
		
		if(e.getSource() == ingredientField)
		{
			ingredientField.selectAll();
		}
		
	}

	
	@Override
	public void focusLost(FocusEvent e)
	{
		// TODO Auto-generated method stub
		
	}
	
}
